package com.training.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.training.dto.Employee;
import com.training.entity.EmpInformation;
import com.training.entity.EmpPayroll;
import com.training.entity.EmplAddress;

public class EmployeeJPADAO {

	EntityManagerFactory entityManagerFactory = null;

	public EmployeeJPADAO() {

		entityManagerFactory = Persistence
				.createEntityManagerFactory("EMP_PERSISTENCE_UNIT");

	}

	public void saveOrUpdateEmployee(EmpInformation empInformation) {
		EntityManager entityManager = entityManagerFactory
				.createEntityManager();

		EntityTransaction transaction = entityManager.getTransaction();

		try {
			transaction.begin();
			entityManager.merge(empInformation);
			transaction.commit();
		} catch (Exception e) {

			transaction.rollback();
			e.printStackTrace();
		}

	}

	public EmpInformation getEmpInformation(String empId)

	{
		EntityManager entityManager = entityManagerFactory
				.createEntityManager();
		EmpInformation empInformation = entityManager.find(
				EmpInformation.class, empId);
		return empInformation;

	}

	public EmpInformation getEmployeeByEmail(String email)

	{
		EntityManager entityManager = entityManagerFactory
				.createEntityManager();
		Query query = entityManager.createNamedQuery("getEmployeeInfo");

		query.setParameter("emailInput", email);

		return (EmpInformation) query.getSingleResult();

	}

	public EmpInformation getEmployee() {

		EntityManager entityManager = entityManagerFactory
				.createEntityManager();
		EmpInformation empInforation = (EmpInformation) entityManager
				.createQuery(
						"Select el from EmpInformation el where email='test@test.com'")
				.getSingleResult();

		return empInforation;
	}

	public static void main(String[] args) {

		EmployeeJPADAO employeeJPADAO = new EmployeeJPADAO();

		EmpInformation empInformation = employeeJPADAO.getEmpInformation("002");
/*		EmpPayroll empPayroll = new EmpPayroll();
		empPayroll.setBillRate(new BigInteger("60"));
		empPayroll.setEmpId("002");
		empPayroll.setNumberWorkHous(170);
		empInformation.setEmpPayroll(empPayroll);
		employeeJPADAO.saveOrUpdateEmployee(empInformation);
*/
		
		
		
		EmplAddress emplAddress = new EmplAddress();
		
		emplAddress.setApartmentNumber("901");
		emplAddress.setCountry("INDIA");
		emplAddress.setCity("HYD");
		emplAddress.setEmpAddressId("4");
		emplAddress.setZipcode("80001");
	//	emplAddress.setEmpInformation(empInformation);
		
//		empInformation.getEmployeeAddress().add(emplAddress);
		
	
		Set<EmplAddress> employeeAddressToInsert = new HashSet<EmplAddress>();
		
		employeeAddressToInsert.add(emplAddress);
		empInformation.setEmployeeAddress(employeeAddressToInsert);
		
		employeeJPADAO.saveOrUpdateEmployee(empInformation);
	 
		 Set<EmplAddress>  emplAddresses   =empInformation.getEmployeeAddress(); 
		
		 for (Iterator<EmplAddress> iterator = emplAddresses.iterator(); iterator.hasNext();) {
			EmplAddress emplAddress2 = (EmplAddress) iterator.next();
			System.out.println(" Employeee   "+empInformation.getEmpFirstName() +"  Lives at  "+emplAddress2.getCity() );
			
			
		}
		 
		
		
	}

}
